﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_SicoNideMillJet : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ValidateForm() != "")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('" + ValidateForm() + "');", true);
            return;
        }
        if (btnSave.CommandArgument == "")
        {
            tbl_siconide_milljet model = new tbl_siconide_milljet();
            model.Mill = Convert.ToInt32(ddlMill.SelectedValue.Trim());
            model.Feed_Rate = Convert.ToDecimal(txtFeed_Rate.Text.Trim());
            model.Charge_Number = Convert.ToDecimal(txtCharge_Number.Text.Trim());
            model.Pressure = Convert.ToDecimal(txtPressure.Text.Trim());
            model.Flowbin_Number = Convert.ToDecimal(txtFlowbin_Number.Text.Trim());
            model.Quantity_Used_MJ1 = Convert.ToDecimal(txtQuantity_Used_MJ1.Text.Trim());
            model.SSA = Convert.ToDecimal(txtSSA.Text.Trim());
            model.Remark = txtRemark.Text.Trim();
            model.Previous_Process = ddlPrevious_Process.SelectedValue.Trim();
            model.PALLA_RUN_NUMBER = ddlPALLA_RUN_NUMBER.SelectedValue.Trim();
            model.MILLJET_RUN_NUMBER = txtMILLJET_RUN_NUMBER.Text.Trim();
            model.CREATED_BY = User.Identity.Name;
            model.CREATED_DATE = DateTime.Now;



            BLL_tbl_siconide_milljet bll = new BLL_tbl_siconide_milljet();
            try
            {


                
                bll.insert(model);
                string script = "top.location=" + '"' + HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + Request.RawUrl + '"';
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('Record Saved Succesfully');", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Postback", script, true);

                return;
            }
            catch (Exception)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured');", true);
                return;
            }
        }
        else
        {
            tbl_siconide_milljet model = new tbl_siconide_milljet();
            model.ID = Convert.ToInt32(btnSave.CommandArgument);
            model.Mill = Convert.ToInt32(ddlMill.SelectedValue.Trim());
            model.Feed_Rate = Convert.ToDecimal(txtFeed_Rate.Text.Trim());
            model.Charge_Number = Convert.ToDecimal(txtCharge_Number.Text.Trim());
            model.Pressure = Convert.ToDecimal(txtPressure.Text.Trim());
            model.Flowbin_Number = Convert.ToDecimal(txtFlowbin_Number.Text.Trim());
            model.Quantity_Used_MJ1 = Convert.ToDecimal(txtQuantity_Used_MJ1.Text.Trim());
            model.SSA = Convert.ToDecimal(txtSSA.Text.Trim());
            model.Remark = txtRemark.Text.Trim();
            model.Previous_Process = ddlPrevious_Process.SelectedValue.Trim();
            model.PALLA_RUN_NUMBER = ddlPALLA_RUN_NUMBER.SelectedValue.Trim();
            model.MILLJET_RUN_NUMBER = txtMILLJET_RUN_NUMBER.Text.Trim();
            model.UPDATED_BY = User.Identity.Name;
            model.UPDATED_DATE = DateTime.Now;



            BLL_tbl_siconide_milljet bll = new BLL_tbl_siconide_milljet();
            bll.update(model);
            btnSave.CommandArgument = "";
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_siconide_milljet model = new tbl_siconide_milljet();
        model.ID = Convert.ToInt32(id); ;

        BLL_tbl_siconide_milljet bll = new BLL_tbl_siconide_milljet();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                BLL_tbl_siconide_milljet bll = new BLL_tbl_siconide_milljet();
                List<tbl_siconide_milljet> list = bll.select(Convert.ToInt32(id));
                foreach (tbl_siconide_milljet tbl in list)
                {
                    btnSave.CommandArgument = tbl.ID.ToString();
                    ddlMill.SelectedValue = tbl.Mill.ToString();
                    txtFeed_Rate.Text = tbl.Feed_Rate.ToString();
                    txtCharge_Number.Text = tbl.Charge_Number.ToString();
                    txtPressure.Text = tbl.Pressure.ToString();
                    txtFlowbin_Number.Text = tbl.Flowbin_Number.ToString();
                    txtQuantity_Used_MJ1.Text = tbl.Quantity_Used_MJ1.ToString();
                    txtSSA.Text = tbl.SSA.ToString();
                    txtRemark.Text = tbl.Remark;
                    ddlPrevious_Process.SelectedValue = tbl.Previous_Process;
                    if (ddlPrevious_Process.SelectedIndex > 0)
                    {
                        DataTable dt = bll.GetPalla_Run_Number();
                        if (ddlPrevious_Process.SelectedValue.Trim() == "Palla")
                        {
                            ddlPALLA_RUN_NUMBER.DataSource = dt;
                            ddlPALLA_RUN_NUMBER.DataTextField = dt.Columns["PALLA_RUN_NUMBER"].ToString();
                            ddlPALLA_RUN_NUMBER.DataValueField = dt.Columns["PALLA_RUN_NUMBER"].ToString();
                            ddlPALLA_RUN_NUMBER.DataBind();
                        }
                        ListItem li = new ListItem("-- Select --", "0");
                        ddlPALLA_RUN_NUMBER.Items.Insert(0,li);
                        ddlPALLA_RUN_NUMBER.SelectedValue = tbl.PALLA_RUN_NUMBER;


                    }
                    
                    txtMILLJET_RUN_NUMBER.Text = tbl.MILLJET_RUN_NUMBER;
                   

                }
            }
        }
    }



    protected void ddlPrevious_Process_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlPALLA_RUN_NUMBER.Items.Clear();
        if (ddlPrevious_Process.SelectedIndex > 0)
        {
            BLL_tbl_siconide_milljet bll = new BLL_tbl_siconide_milljet();
            DataTable dt = bll.GetPalla_Run_Number();
            if (ddlPrevious_Process.SelectedValue.Trim() == "Palla")
            {
                ddlPALLA_RUN_NUMBER.DataSource = dt;
                ddlPALLA_RUN_NUMBER.DataTextField = dt.Columns["PALLA_RUN_NUMBER"].ToString();
                ddlPALLA_RUN_NUMBER.DataValueField = dt.Columns["PALLA_RUN_NUMBER"].ToString();
                ddlPALLA_RUN_NUMBER.DataBind();
            }
            ListItem li = new ListItem("-- Select --", "0");
            ddlPALLA_RUN_NUMBER.Items.Insert(0, li);
            return;


        }
    }
    protected void ddlMill_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("--Select--", "0");
        ddlMill.Items.Insert(0, li);
    }

    #region Validate Form

    private string ValidateForm()
    {
        string str = "";
        try
        {
            // Check if Client is Selected 
            if (ddlMill.SelectedIndex == 0) return "Please select Mill";
            if (txtFeed_Rate.Text == "") return "Please enter Feed Rate";
            if (txtCharge_Number.Text == "") return "Please enter Charge Number";
            if (txtPressure.Text == "") return "Please enter Pressure";
            if (txtFlowbin_Number.Text == "") return "Please enter Flowbin Number";


            if (txtQuantity_Used_MJ1.Text == "") return "Please enter Quantity Used MJ1";
            if (txtSSA.Text == "") return "Please enter SSA";


            if (txtRemark.Text == "") return "Please enter Remark";
            if (ddlPrevious_Process.SelectedIndex == 0) return "Please select Previous Process";
            if (txtMILLJET_RUN_NUMBER.Text == "") return "Please enter MILLJET RUN NUMBER";

            return str;
        }
        catch (Exception)
        {

            return "Error in Validation";
        }
        finally
        {
            str = null;
        }
    }

    #endregion
}